package com.ykdl.member.kid.regandlogin;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import net.wxxr.dataparse.model.DataParseCode;
import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import net.wxxr.http.util.Constants;
import oauth.signpost.http.HttpParameters;
import android.content.Intent;
import android.graphics.ColorFilter;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.beans.ResetPasswordBean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.regandlogin.ForgetPwActivity.resetpasswordTag;
import com.ykdl.member.kid.regandlogin.PhoneNumberRegisterOneActivity.MyCount;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class ForgetPwOneActivity extends BaseScreen implements OnClickListener{
	private Button sen_bt;
	private EditText authcode;
	private TextView phone_notice ;
	String phonesString = "" ;
	private MyCount mc ;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("忘记密码", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				finish();
			}
		}, R.string.turn_ok, 0, new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (authcode.getText().toString().length()==6) {
					verifyCode();
				}else {
					Toast.makeText(mContext, "验证码有误，请重新输入", 1).show();
				}
			}
		}, R.drawable.title_bg, false);
		setContent(R.layout.forgetpwone_xml);
		findbyid();
		phonesString = this.getIntent().getStringExtra("phone_number");
		phone_notice.setText("验证信息已发送到"+phonesString);
	}
	public void findbyid() {
		phone_notice = (TextView)findViewById(R.id.notice_phone_number);
		phone_notice.setOnClickListener(this);
		sen_bt = (Button)findViewById(R.id.sen_bt);
		sen_bt.setOnClickListener(this);
		authcode = (EditText)findViewById(R.id.edit_phone_number);
		authcode.setInputType(EditorInfo.TYPE_CLASS_PHONE);
		 mc = new MyCount(60000, 1000);
		mc.start();
	}
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.edit_phone_number:
			 mc = new MyCount(60000, 1000);
				mc.start();
			break;
		case R.id.sen_bt:
			if(isConnect(this)){
				goRestPw(phonesString);
			}else{
				Toast.makeText(mContext, "请连接网络", 1).show();
				return;
			}
			
			break;

		default:
			break;
		}
	}
	public void goNext() {
		Intent intent = new Intent(this,ForgetPwTwoActivity.class);
		intent.putExtra("phone_number", phonesString);
		intent.putExtra("verifyCode",authcode.getText().toString());
		startActivity(intent);
		ForgetPwOneActivity.this.finish();
	}
	public void verifyCode () {
		showProgress("正在发送请稍等...");
		String  pageurl = KidConfig.PHONE_CONFIRM_VERIFY;
		HttpParameters bodyParams = new HttpParameters();
		String codeString = null;
		try {
			codeString = URLEncoder.encode(authcode.getText().toString(), "utf8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		bodyParams.put("app_key", "hsh");
		bodyParams.put("phone", phonesString);
		bodyParams.put("confirm_code", codeString);
		HttpBaseRequest request  =  Wxxr.getInstance().postBaseRequest(pageurl,bodyParams,null);
		 TaskManager.startHttpRequest(request, new resetpasswordTag(),  ResetPasswordBean.class);
		
	}
	class resetpasswordTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof ResetPasswordBean) {
				
				Log.e("alan", "-------t:"+t.toString());
				ResetPasswordBean bean = (ResetPasswordBean)t;
				if (bean.getError()!=null) {
						Toast.makeText(mContext, bean.getDesc(), 1).show();
				}else {
					if (bean.getStatus()==0) {
						Toast.makeText(mContext, "成功", 1).show();
						Log.e("alan", "-------t:"+t.toString());
						goNext() ;
					}else {
						Toast.makeText(mContext, "失败"+bean.getStatus(), 1).show();
					}
					
				}
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			String errMsg = parseError.getErrMsg();
			DataParseCode errorCodeString = parseError.getErrorCode();
			finishProgress();
			Toast.makeText(mContext, "参数有误失败", 1).show();
			return true;
			
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	public void goRestPw (String phoneString) {
		showProgress("正在发送请稍等...");
		String  pageurl = KidConfig.REGISTER_BYPHONE;
		HttpParameters parameterss = new HttpParameters();
		parameterss.put("app_key", "hsh");
		parameterss.put("phone", phoneString);
		parameterss.put("client_id", Constants.CLIENT_ID);
		parameterss.put("client_secret", Constants.CLIENT_SECRET);
		parameterss.put("type", "password");
		HttpBaseRequest request  =  Wxxr.getInstance().postBaseRequest(pageurl,parameterss, null);
		 TaskManager.startHttpRequest(request, new getverifyCodeTag(),  ResetPasswordBean.class);
		
	}
	class getverifyCodeTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			if (t instanceof ResetPasswordBean) {
				finishProgress();
				ResetPasswordBean bean = (ResetPasswordBean)t;
				if (bean.getError()!=null) {
						Toast.makeText(mContext, "失败"+bean.getError(), 1).show();
				}else {
					if (bean.getStatus()==0) {
						Toast.makeText(mContext, "成功", 1).show();
						Log.e("alan", "-------t:"+t.toString());
						 mc = new MyCount(60000, 1000);
							mc.start();
//						gonext() ;
					}else {
						Toast.makeText(mContext, "失败"+bean.getStatus(), 1).show();
					}
					
				}
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "参数有误失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
		}
		
	}
	class MyCount extends CountDownTimer { 
		public MyCount(long millisInFuture, long countDownInterval) { 
		super(millisInFuture, countDownInterval); 
		} 
		@Override
		public void onFinish() { 
			sen_bt.setText("重发"); 
			sen_bt.setTextColor(0xFFFFA800);
			sen_bt.setEnabled (true);
		} 
		@Override
		public void onTick(long millisUntilFinished) { 
			sen_bt.setEnabled (false);
			sen_bt.setTextColor(0xFF9D9D9D);
			sen_bt.setText("请等待(" + millisUntilFinished / 1000 + ")秒"); 
		} 
		}
	

}
